$(document).ready(function() {

//follow plugin
//	$('#cart').jfollow('#followbox', 20);	


	//shopping cart functionality
	//hide the empty cart button
	emptyBtn.hide();
	emptyBtn2.hide();
	
	//make the product class div part of the draggable ui
	
	$('.product').draggable({
		appendTo: 'body',
		helper: 'clone'		
	});

	//make the dropzone class div a part of the droppable ui
	$('.dropzone').droppable({
		tolerance: 'touch',
		activeClass: 'ui-state-default',
		hoverClass: 'ui-state-hover',
		accept: '.product',
		drop: function (event, ui) {
			addItem(ui.draggable);
		}
	});
	
	$('input[name="quantity"]').live('change', function(){
	
		if (isNaN(this.value)) $(this).val(1);
		if (parseInt(this.value)==0) $(this).val(1);
		var _st = subTotal();
		$("li#subtotal > span").text(_st.toFixed(2));
		$("li#total > span").text((_st + 4.0).toFixed(2));
		//$("#txtpaga").val((_st + 4.0).toFixed(2));
	});
	
	$('a.more').live('click', function() {
		$(this).parent().find('input').val(parseInt($(this).parent().find('input').val()) + 1);
		var _st = subTotal();
		$("li#subtotal > span").text(_st.toFixed(2));
		$("li#total > span").text((_st + 4.0).toFixed(2));
		//$("#txtpaga").val((_st + 4.0).toFixed(2));
	});
	
	$('a.less').live('click', function() {
		$(this).parent().find('input').val(parseInt($(this).parent().find('input').val()) - 1);
		if (parseInt($(this).parent().find('input').val())==0) $(this).parent().find('input').val(1);
		
		var _st = subTotal();
		$("li#subtotal > span").text(_st.toFixed(2));
		$("li#total > span").text((_st + 4.0).toFixed(2));
		//$("#txtpaga").val((_st + 4.0).toFixed(2));
	});

}); //end document ready

    function remove(el) {
        $(el).hide();
        $(el).parent().parent().fadeOut('1000');
		
		
		// update total item
        total_items--;
        if( total_items === 0){
			emptyText.delay('1000').fadeIn('500');
			emptyBtn.fadeOut('500');
			emptyBtn2.fadeOut('500');
			$("li#total > span").text("0.00");
			}
		
        setTimeout(function() {
            $(el).parent().parent().remove();
            
            var _st = subTotal();
    		$("li#subtotal > span").text(_st.toFixed(2));
    		$("li#total > span").text((_st + (total_items>0?4.0:.0)).toFixed(2));
			//$("#txtpaga").val((_st + 4.0).toFixed(2));
            
        }, 1000);

        
        
        
        
        
  	}
    
    function subTotal() {
    	var st= .0;
    	$('.cart-item').each(function(a,ele) {
    		st = st + (parseFloat($(ele).attr('data-price')) * parseInt($(ele).find('input').val())) ;
    	});
    	return st ;
    }
    
    
    
    function addItem(ele){
		var item = $(ele).find('.product-title').text();
		var itemid = $(ele).find('.id').text();
		var price = $(ele).find('.price').text();
		quantity = parseInt($(ele).find('input#q').val());
		
		var asset = [];
		jQuery.each($(ele).find('form#asset').serializeArray(), function(i, field){
			asset.push(field.value);
		});
		
		
		var html = '<div class="cart-item" data-productid="'+itemid+'" data-price="'+ price +'" data-asset="'+ asset.join() +'" >';
			html = html + '<div class="div-remove">';
			html = html + '<a onclick = "remove(this)" class="remove '+itemid+'">&times;</a>'+'</div>';
			html = html + '<p class="item-name">'+item+'</p>';
			html = html + '<p  class="item-price">S/. '+price+'</p>';
			//html = html + '<p class="input">'+'<input type="text" maxlength="2" name="quantity" value="'+quantity+'" />';
			html = html + '<p class="input">'+'<a class="less" href="javascript:void(0);">-</a>&nbsp;<input type="number" min="1" max="99" step="1" value="'+quantity+'" name="quantity" style="width:40px; text-align:right; "  />';
			html = html + '&nbsp;<a href="javascript:void(0);" class="more" >+</a></p>'+'</div>'
		
		var cartitem = $('".cart-item[data-productid="'+itemid+'"]"');
		
		if(cartitem.length > 0){
			var int = parseInt(cartitem.find('input').val());
			int ++;
			cartitem.find('input').val(int);	
		}else{
			var content = $('.cart-content');    
			content.append(html);
			if ($('.cart-item').length == 1)
				content.next().html('<ul ><li id="subtotal" >Sub Total: S/. <span>0.00</span> </li>'+
						'<li>Servicio Delivery: S/. 4.00 </li>' + 
						'<li id="total" >Total a pagar: S/. <span>0.00</span> </li>' + 
					'</ul>');
			emptyText.hide();
		}			
		//update the total items
		total_items++;			
		if(total_items > 0){
			emptyBtn.fadeIn('1000');
			emptyBtn2.fadeIn('1000');
		}
		
		var _st = subTotal();
		$("li#subtotal > span").text(_st.toFixed(2));
		$("li#total > span").text((_st + 4.0).toFixed(2));
		//$("#txtpaga").val((_st + 4.0).toFixed(2));
		
		
		emptyBtn.click(function(){
			$('#dialog-confirm').dialog({
				resizable: false,
				modal: true,
				buttons: [	{	
							text: "Cancelar",
							click: function(){
								$(this).dialog('close');}
							},
							{	
							text: "Limpiar",
							click: function(){
								var content = $('.cart-item');
								content.remove();
								$('cart-content').find('.placeholder').show();
								$(this).dialog('close');
								emptyBtn.fadeOut('500');
								emptyBtn2.fadeOut('500');
								emptyText.fadeIn('500');
								$('.totales').html("");
								//$("#txtpaga").val("");
								}
							}
						]					
			})
			return false;
		});
		
		
		emptyBtn2.click(function() {
			  $.lightbox("#loremipsum", {
				'width'       : 340,
				'height'      : 360,
				'autoresize'  : true, 'modal' : true,
				//'onOpen'  	  : function() {$("#txtpaga").val((subTotal() + 4.0).toFixed(2));},
			  });
			  return false;
		    }
		);
		
	} //end drop function
    
    
    
    
    
var total_items = 0;	

var emptyText = $('.cart-content').find('.placeholder');
var emptyBtn = $('.empty').button();
var emptyBtn2 = $('.empty2').button();
var quantity = 0;
	




